<?PHP
//written by Slyferz
session_start();
error_reporting(E_ALL);

//getting the user's access to see if they are allowed
include("includes/paxsql.php");
//$userid = $_SESSION['userid'];
//include("../recruitment/toolAccess.php");
//$access = getAcc('snowflake',$userid);
mysql_close();

require_once('api/class.api.php');
require_once('api/class.charselect.php');
include 'includes/database.php';

//getting the kills for all characters
//player[][0] = kills, [][1] = month, [][2] = trend, [][3] = tracking array
function getKills($charID)
{
	$month = gmdate('m'); // cyan: date to gmdate
	$year = gmdate('Y'); // cyan: date to gmdate
	//$year = "20".date('y');
	$player[0][1] = $month;
	$number = 0;
	$chars = 0;
	//echo $charID." ";
	for($i = 0; $i < 6; $i++)
	{
		//making sure december processes correctly
		if($month == 12 && $i != 0)
		{
			$year--;
			$month1 = 1;
		}
		else if($month == 12 && $i == 0)
		{
			$check = 1;
		}
		else
		{
			$check = 0;
			$month1 = 0;
		}	
		//echo $year." ";
		//getting the span of the current month
		$time1 = gmmktime(0,0,0, $month, 1, $year);
		//$time1 += (9*60*60); // cyan: commented this out
		//echo " ".$time1." ";
		if($month < 12 || $check == 1)$month++;
		else
		{
			$month -= 11;
			//$month = 1;
		}
		if($month1 == 1)
			$year++;
	//echo $year." ";	
		$time2 = gmmktime(0,0,0, $month, 0, $year);
		$time2 += (9*60*60);
		//echo $time2."|||";
		if($month1 == 1)
			$year--;
		//decreasing in months and adjusting the year
		//if it is next year
		if($month > 2) $month -= 2;
		else if($month == 2)
		{
		       	$month = 12;
			$year = date('Y');
		}
		else $month = 11;
		
		//getting the join information for the character
		$result = mysql_query("SELECT joined FROM characters WHERE charID = '".$charID."'");
		$row = mysql_fetch_array($result);
		$joined = explode(' ',$row[0]);
		
		//getting number of kills grouping by pilot id
		$result = mysql_query("SELECT COUNT(*) as total, pilot_id, system_id FROM kills WHERE main_id = $charID AND timestamp >= $time1 AND timestamp < $time2 GROUP BY pilot_id") or die(mysql_error());
		for($j = 0;$row = mysql_fetch_array($result); $j++) 
		{
			if($time2 < ($joined[0] - (1*24*60*60))) $player[$i][0] = '*';
			else $player[$i][0] += $row["total"];
			//getting the information for the individual characters
			$tracking[$j][$number] = $row['system_id'];
			$tracking[$j][$number+1] = $row['total'];
			$tracking[$j][$number+2] = $row['pilot_id'];
			$chars++;
		}	
		$number += 3;
		//the months
		if($player[$i][1] > 1) $player[($i+1)][1] = ($player[$i][1] - 1);
		else $player[($i+1)][1] = 12;
	}
	$player[0][3] = $tracking;
	$player[0][4] = $chars;
	return $player;
}

if($access > 1)
{
	//getting the kills from the main and alternate characters
	$charID = addslashes($_GET["charID"]);
	$player = getKills($charID);
	$result = mysql_query("SELECT name FROM characters WHERE charID = '".$charID."'");
	$row = mysql_fetch_array($result);
	echo("<div style = 'text-align:center;font-weight:bold;'>".$row[0]."</div>");
	
	//displaying the months
	echo("<table style = 'margin:0 auto;' border = '1'>
		<tr><td>Months</td>");
	
	for($i = 6; $i > 0; $i--)echo("<td style='text-align:center;'>".date("F",mktime(0,0,0,$player[$i - 1][1]))."</td>");
	echo("</tr>");
	
	//checking the trend from month to month
	for($i = 6; $i > 0; $i--)
	{
		if($player[$i][0] < $player[($i - 1)][0]) $player[$i][2]="<img src = 'images/up.png' alt = 'up' />";
		else if($player[$i][0] > $player[($i - 1)][0]) $player[$i][2]="<img src = 'images/down.png' alt = 'down' />";
	}
	
	//displaying the trends
	echo("<tr><td>Trend</td><td></td>");
	for($i = 6; $i > 1; $i--) echo("<td style='text-align:center;'>".$player[($i - 1)][2]."</td>");
	for($i = 0; $i < 6; $i++) if(empty($player[$i][0])) $player[$i][0] = 0;
	
	//displaying the kills
	echo("</tr><tr><td>Kills</td>");
	for($i = 6; $i > 0; $i--)
	{
		echo("<td style='text-align:center;'>".$player[($i - 1)][0]."</td>");
	}	
	echo "</tr>";
	
	$tracking = $player[0][3];
	//sorting the kills
	echo("<table style = 'margin:0 auto;' border = 0;>");
	$j = 6;
	for($m = 17; $m > 0; $m -= 3)
	{
		//spacing out the tables
		if($m == 17 || $m == 11 || $m == 5) echo("<tr>");
		echo("<td>");
		//tracking[][0] = system_id, [][1] = total, [][2] = pilot_id
		for($i = 0; $i < $player[0][4]; $i++)
		{
			//recording the kills for each individual pilot
			$charKills[$tracking[$i][$m]][0] += $tracking[$i][$m - 1];
			$count += $tracking[$i][$m - 1];	
		}
	
		//showing kills per character and region %
		echo("<table style = 'margin:0 auto;' border = 1>");
		echo("<tr><td style = 'font-weight:bold;'>".date("F",mktime(0,0,0,$player[$j - 1][1]))."</td><td>".$count."</td></tr>");
		echo("<tr><td>Character</td><td>Kills</td></tr>");//<td>Regions</td><td> % </td></tr>");
		//getting all pilots that have made a kill in the past 6 months
		$result = mysql_query("SELECT distinct pilot_id FROM kills WHERE main_id = '".$charID."'");
		for($i = 0; $row = mysql_fetch_array($result); $i++)
		{
			//getting the names of the characters
			$char = mysql_query("SELECT name FROM characters WHERE realID = '".$row[0]."'");
			$name = mysql_fetch_array($char);
			//just adding a 0 to null to make it look pretty
			if(empty($charKills[$row[0]][0])) $charKills[$row[0]][0] = 0;
			echo("<tr><td style>".$name[0]."</td><td>".$charKills[$row[0]][0]."</td></tr>");//<td></td><td></td></tr>");
			$charKills[$row[0]][0] = 0;
		}
		$count = 0;
		echo("<br />");
		echo("</table>");
		echo("</td>");
		if($m == 14 || $m == 8 || $m == 2) echo("</tr>");
		$j--;
	}
	echo("</table
		<p style = 'text-align: center;'><a href = 'http://".$_SERVER['HTTP_HOST']."/members.php'>< Back</a></p>");
	
	//echo("join date: ".$joined[0]." and again: ".date('Y-m-d', $joined[0])."month: ".date('F',$joined[0]));
	//echo("<div style = 'text-align:center;'>Work in progress.</div>");
}
else
{
	header("Location: http://".$_SERVER['HTTP_HOST']."/index.php");
}
	
mysql_close();
?>
